package com.smart.service.impl;

import com.smart.pojo.Department;
import com.smart.mapper.DepartmentMapper;
import com.smart.pojo.Role;
import com.smart.pojo.User;
import com.smart.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.service.IRoleService;
import com.smart.service.IUserService;
import com.smart.utils.SMUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyj
 * @since 2023-04-19
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private IRoleService roleService;

    @Autowired
    IUserService userService;

    @Override
    @Transactional
    public boolean addDept(Department department) {
        int insertDept = baseMapper.insert(department);
        //生成部门编号
        department.setCode(SMUtils.getDeptCode(department.getId()));
        baseMapper.updateById(department);

        if (insertDept > 0) {
            //部门添加成功，设置默认部门角色
            Role leader = new Role();
            leader.setName(department.getName() + "部长");
            leader.setIsDepartmentRole(1);
            leader.setDepartmentId(department.getId());
            leader.setIsLeader(1);

            Role simpleEmployee = new Role();
            simpleEmployee.setName(department.getName() + "专员");
            simpleEmployee.setIsDepartmentRole(1);
            simpleEmployee.setDepartmentId(department.getId());

            boolean saveLeader = roleService.save(leader);
            boolean saveEmp = roleService.save(simpleEmployee);

            //将部门领导赋予部长角色
            boolean associate = userService.lambdaUpdate()
                    .set(User::getRoleId, leader.getId())
                    .eq(User::getId, department.getLeader())
                    .update();

            return saveEmp & saveLeader & associate;
        } else {
            return false;
        }

    }

    @Override
    @Transactional
    public boolean updateDept(Department department) {
        Department oldDepartment = baseMapper.selectById(department.getId());

        //检查部长用户变更
        if (oldDepartment.getLeader() != department.getLeader()) {
            //先解除原部长用户关系
            User oldUser = userService.getById(oldDepartment.getLeader());
            Long LeaderRoleId = oldUser.getRoleId();
            oldUser.setRoleId(null);
            boolean updateOldUser = userService.updateById(oldUser);

            //关联新任部长用户
            User newUser = userService.getById(department.getLeader());
            newUser.setRoleId(LeaderRoleId);
            boolean updateNewUser = userService.updateById(newUser);
        }

        return baseMapper.updateById(department) > 0;
    }

    @Override
    public Department queryByUserId(Long userId) {
        return baseMapper.queryByUserId(userId);
    }

}
